package org.bitmaps.cutBitmap.view
{
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.ui.Keyboard;
	
	/**
	 * 截图工具栏  
	 * 画椭圆 画矩形  画箭头 保存等按钮面板
	 * @author anjue
	 */
	public class DrawToolPanel extends Sprite
	{
		public static const DRAW_RECT:uint = 1;
		public static const DRAW_ELLIPSE:uint = 2;
		public static const DRAW_ARROW:uint = 3;
		
		public var drawFun:Function;
		public var undoFun:Function;
		public var completeFun:Function;
		public var colorFun:Function;
		public var exitFun:Function;
		
		private var main:UI_001_CutTool;
		public var color:uint = 0;
		private var colorVec:Vector.<uint>;
		public function DrawToolPanel()
		{
			main = new UI_001_CutTool();
			this.addChild(main);
			main.mc_tool.btn_rect.addEventListener(MouseEvent.CLICK,rectClick);
			main.mc_tool.btn_ellipse.addEventListener(MouseEvent.CLICK,ellipseClick);
			main.mc_tool.btn_arrow.addEventListener(MouseEvent.CLICK,arrowClick);
			main.mc_tool.btn_back.addEventListener(MouseEvent.CLICK,backClick);
			main.mc_tool.btn_exit.addEventListener(MouseEvent.CLICK,exitClick);
			main.mc_tool.btn_sub.addEventListener(MouseEvent.CLICK,subClick);
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			
			initColor();
			main.mc_tool.btn_rect.gotoAndStop(1);
			main.mc_tool.btn_ellipse.gotoAndStop(1);
			main.mc_tool.btn_arrow.gotoAndStop(1);
		}
		
		private function addToStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
		}
		
		private function initColor():void
		{
			colorVec = new Vector.<uint>();
			main.mc_color.visible = false;
			var i:uint = 0;
			for(i;i<16;i++)
			{
				var item:MovieClip = main.mc_color["color_"+i];
				item.addEventListener(MouseEvent.CLICK,colorClick);
				item.buttonMode = true;
				var bmd:BitmapData = new BitmapData(item.width,item.height,true,0);
				bmd.draw(item);
				colorVec.push(bmd.getPixel(5,5));
				bmd.dispose();
			}
		}
		
		private function colorClick(e:MouseEvent):void
		{
			var index:uint = String(e.currentTarget.name).split("_")[1];
			var _color:uint = colorVec[index];
			var ct:ColorTransform = new ColorTransform();
			ct.color = _color;
			main.mc_color.main_color.transform.colorTransform = ct;
			if(Boolean(colorFun))
			{
				colorFun(_color);
			}
		}
		
		private function rectClick(e:MouseEvent):void
		{
			if(Boolean(drawFun))
			{
				drawFun(DRAW_RECT);
			}
			main.mc_color.visible = true;
			main.mc_tool.btn_rect.gotoAndStop(2);
			main.mc_tool.btn_ellipse.gotoAndStop(1);
			main.mc_tool.btn_arrow.gotoAndStop(1);
		}
		
		private function ellipseClick(e:MouseEvent):void
		{
			if(Boolean(drawFun))
			{
				drawFun(DRAW_ELLIPSE);
			}
			main.mc_color.visible = true;
			main.mc_color.visible = true;
			main.mc_tool.btn_rect.gotoAndStop(1);
			main.mc_tool.btn_ellipse.gotoAndStop(2);
			main.mc_tool.btn_arrow.gotoAndStop(1);
		}
		
		private function arrowClick(e:MouseEvent):void
		{
			if(Boolean(drawFun))
			{
				drawFun(DRAW_ARROW);
			}
			main.mc_color.visible = true;
			main.mc_color.visible = true;
			main.mc_tool.btn_rect.gotoAndStop(1);
			main.mc_tool.btn_ellipse.gotoAndStop(1);
			main.mc_tool.btn_arrow.gotoAndStop(2);
		}
		
		private function backClick(e:MouseEvent):void
		{
			if(Boolean(undoFun))
			{
				undoFun();
			}
		}
		
		private function exitClick(e:MouseEvent):void
		{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDown);
			if(Boolean(exitFun))
			{
				exitFun();
			}
		}
		
		private function subClick(e:MouseEvent):void
		{
			if(Boolean(completeFun))
			{
				completeFun();
			}
		}
		
		public function showColor():void
		{
			main.mc_color.visible = true;
		}
		
		public function hideColor():void
		{
			main.mc_color.visible = false;
		}
		
		
		private function keyDown(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.ESCAPE)
			{
				stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDown);
				if(Boolean(exitFun))
				{
					exitFun();
				}
			}
		}
		
		public function destroy():void
		{
			main.mc_tool.btn_rect.removeEventListener(MouseEvent.CLICK,rectClick);
			main.mc_tool.btn_ellipse.removeEventListener(MouseEvent.CLICK,ellipseClick);
			main.mc_tool.btn_arrow.removeEventListener(MouseEvent.CLICK,arrowClick);
			main.mc_tool.btn_back.removeEventListener(MouseEvent.CLICK,backClick);
			main.mc_tool.btn_exit.removeEventListener(MouseEvent.CLICK,exitClick);
			main.mc_tool.btn_sub.removeEventListener(MouseEvent.CLICK,subClick);
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			if(stage)
			{
				stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDown);
			}
		}
	}
}